package com.elm.controller;

import com.elm.common.ApiResponse;
import com.elm.po.Business;
import com.elm.service.BusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api")
public class BusinessController {

    @Autowired
    private BusinessService businessService;
    
    @PostMapping("/businesses")
    public ApiResponse<Business> createBusiness(
            @RequestBody @Validated Business business) {
        try {
            int result = businessService.createBusiness(business);
            if (result <= 0) {
                return ApiResponse.error("500", "商家创建失败");
            }
            Business createdBusiness = businessService.getBusinessByName(business.getBusinessName());
            return ApiResponse.success(createdBusiness);
        } catch (RuntimeException e) {
            return ApiResponse.error("400", e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error("500", "系统错误：" + e.getMessage());
        }
    }


    @GetMapping("/business/{orderTypeId}")
    public ApiResponse<List<Business>> getBusinessByOrderTypeId(@PathVariable Integer orderTypeId) {
        try {
            List<Business> result = businessService.getBusinessByOrderTypeId(orderTypeId);
//            if (result == null) {
//                return ApiResponse.error("500", "获取类型商家失败");
//            }
            return ApiResponse.success(result);
        } catch (RuntimeException e) {
            return ApiResponse.error("400", e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error("500", "系统错误：" + e.getMessage());
        }
    }

    @GetMapping("/businesses/{id}")
    public ApiResponse<Business> getBusinessById(@PathVariable Integer id) {
        try {
            Business business = businessService.getBusinessById(id);
            if (business == null) {
                return ApiResponse.error("404", "未找到指定商家");
            }
            return ApiResponse.success(business);
        } catch (RuntimeException e) {
            return ApiResponse.error("400", e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error("500", "系统错误：" + e.getMessage());
        }
    }
    
    @PatchMapping("/businesses/{id}")
    public ApiResponse<Business> patchBusiness(@PathVariable Integer id,@RequestBody @Validated Business business) {
        try {
            Business business1 = businessService.updateBusiness(business,id);
            if (business1 == null) {
                return ApiResponse.error("404", "更新指定商家失败");
            }
            return ApiResponse.success(business1);
        } catch (RuntimeException e) {
            return ApiResponse.error("400", e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error("500", "系统错误：" + e.getMessage());
        }
    }
    
    @GetMapping("/businesses")
    public ApiResponse<List<Business>> getBusiness() { 
        try {
            List<Business> businesses = businessService.getAllBusiness();
            if(businesses == null) {
            	return ApiResponse.error("404", "未找到商家");
            }
            return ApiResponse.success(businesses);
        } catch (RuntimeException e) {
            return ApiResponse.error("400", e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error("500", "系统错误：" + e.getMessage());
        }
    }
    
    @GetMapping("/businesses/{userId}/boss")
    public ApiResponse<List<Business>> getBusinessByBoss(@PathVariable Integer userId) { 
        try {
            List<Business> businesses = businessService.getBusinessByBoss(userId);
            if(businesses == null) {
            	return ApiResponse.error("404", "未找到商家");
            }
            return ApiResponse.success(businesses);
        } catch (RuntimeException e) {
            return ApiResponse.error("400", e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error("500", "系统错误：" + e.getMessage());
        }
    }
    
    @GetMapping("/businesses/all")
    public ApiResponse<List<Business>> getAllBusiness() { 
        try {
            List<Business> businesses = businessService.getAllBusiness();
            if(businesses == null) {
            	return ApiResponse.error("404", "未找到商家");
            }
            return ApiResponse.success(businesses);
        } catch (RuntimeException e) {
            return ApiResponse.error("400", e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error("500", "系统错误：" + e.getMessage());
        }
    }

    @GetMapping("/businesses/sales")
    public ApiResponse<List<Business>> getBusinessBySales() { 
        try {
            List<Business> businesses = businessService.getBusinessBySales();
            if(businesses == null) {
            	return ApiResponse.error("404", "未找到商家");
            }
            return ApiResponse.success(businesses);
        } catch (RuntimeException e) {
            return ApiResponse.error("400", e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error("500", "系统错误：" + e.getMessage());
        }
    }

    @GetMapping("/businesses/star")
    public ApiResponse<List<Business>> getBusinessByStar() {
        try {
            List<Business> businesses = businessService.getBusinessByStar();
            return ApiResponse.success(businesses);
        } catch (RuntimeException e) {
            return ApiResponse.error("400", e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error("500", "系统错误：" + e.getMessage());
        }
    }

    @GetMapping("/businesses/reviewing")
    public ApiResponse<List<Business>> getAllReviewingBusiness() {
        try {
            List<Business> businesses = businessService.getAllReviewingBusiness();
//            if(businesses == null) {
//            	return ApiResponse.error("404", "未找到未审核的商家");
//            }
            return ApiResponse.success(businesses);
        } catch (RuntimeException e) {
            return ApiResponse.error("400", e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error("500", "系统错误：" + e.getMessage());
        }
    }

    @PutMapping("/businesses/{id}/status/{result}")
    public ApiResponse<Business> updateBusinessStatus(@PathVariable Integer id, @PathVariable String result) {
        try {
            Business updatedBusiness = businessService.updateBusinessStatus(id, result);
            if (updatedBusiness == null) {
                return ApiResponse.error("500", "更新商家状态失败");
            }
            return ApiResponse.success(updatedBusiness);
        } catch (RuntimeException e) {
            return ApiResponse.error("400", e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error("500", "系统错误：" + e.getMessage());
        }
    }

    
    @DeleteMapping("/businesses/{id}")
    public ApiResponse<Business> deleteBusiness(@PathVariable Integer id) {
        try {
            Business updatedBusiness = businessService.updateBusinessStatus(id, "offline");
            if (updatedBusiness == null) {
                return ApiResponse.error("500", "删除商家失败");
            }
            return ApiResponse.success(updatedBusiness);
        } catch (RuntimeException e) {
            return ApiResponse.error("400", e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error("500", "系统错误：" + e.getMessage());
        }
    }
    
    @PutMapping("/businesses/{id}/update")
    public ApiResponse<Business> updateBusiness(@RequestBody Business business, @PathVariable Integer id) {
        try {
            Business updatedBusiness = businessService.updateBusiness(business, id);
            if (updatedBusiness == null) {
                return ApiResponse.error("404", "更新商家信息失败，未找到指定商家");
            }
            return ApiResponse.success(updatedBusiness);
        } catch (RuntimeException e) {
            return ApiResponse.error("400", e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error("500", "系统错误：" + e.getMessage());
        }
    }

}
